<% include header.ejs %>
    <div id="page-wrapper">
        <div class="row">
            <div class="col-lg-12">
                <h1 class="page-header">产品/订单管理</h1>
            </div>
            <table id="orderTable" class="table dataTable datatable_ table-striped table-bordered table-condensed">
                <thead>
                    <th>id</th>
                    <th>产品id</th>
                    <th>数量</th>
                    <th>产品名字</th>
                    <th>创建日期</th>
                    <th>属性</th>
                    <th>总价</th>
                    <th>状态</th>
                    <th>下单人</th>
                    <th>操作</th>
                    <!--<th>描述</th>-->
                </thead>
                <tbody></tbody>
            </table>
        </div>

    </div>
    <!-- /#page-wrapper -->

    </div>
    <!-- /#wrapper -->
    <% include footer.ejs %>

</html>
<script>
    $(function(){
        orderTableAjax()
    })
    function orderTableAjax(){
            $.ajax({
                url: 'order/findAll',
    //            data:'',
                method: 'post',
                success: function (res) {
                    console.log(res)
                    orderTable(res)
                },
                error: function () {

                }
            })
        }
    function orderTable(data){
        $('#orderTable').DataTable({
            destroy: true,
            "bAutoWidth": false,
            responsive: true,
            data: data,
            "aoColumnDefs": [
                { "bSortable": false, "aTargets": [ 4,5,8,9] },
                {
                    "render": function(data, type, row) {
                        return (new Date(parseInt(data))).toLocaleString();
                    },
                    "targets": 4
                },
                {
                    "render": function(data, type, row) {

                        return data==0?'未批准':'已批准'
                    },
                    "targets":7
                },
                {
                    "render": function(data, type, row) {
                        return findName(data)
                    },
                    "targets":8
                },
                {
                    "render": function(data, type, row) {

                        var html;
                        if(row.status == 0){
                            html = "<button data-id="+data.id+" class='btn btn-sm btn-primary' data-toggle='modal' " +
                                "data-target='#userUpdateModel' onclick='userUpdateModel(this)'>批准订单</button>"+
                                "<button data-id="+data.id+" class='btn btn-sm btn-danger' onclick='deleteUser(this)" +
                                "'>删除</button>"
                        }
                        else{
                            html = "<button data-id="+data.id+" class='btn btn-sm btn-info' data-toggle='modal' " +
                                "data-target='#userUpdateModel' onclick='chexiao(this)'>撤销订单</button>"+
                                "<button data-id="+data.id+" class='btn btn-sm btn-danger' onclick='deleteUser(this)" +
                                "'>删除</button>"
                        }
                        return html
                    },
                    "targets": 9
                },
            ],
            columns: [
                {
                    data: 'id'
                },{
                    data: 'prodId'
                },{
                    data: 'quan'
                },{
                    data: 'prodName'
                },{
                    data: 'ct'
                },{
                    data: 'attr'
                },{
                    data: 'total'
                },{
                    data: 'status',
                },
                {
                    data: 'userId',
                },
                {
                    data: null
                },
            ],
            language: {
                "sProcessing": "处理中...",
                "sLengthMenu": "显示 _MENU_ 项结果",
                "sZeroRecords": "没有匹配结果",
                "sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
                "sInfoEmpty": "显示第 0 至 0 项结果，共 0 项",
                "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
                "sInfoPostFix": "",
                "sSearch": "搜索:",
                "sUrl": "",
                "sEmptyTable": "表中数据为空",
                "sLoadingRecords": "载入中...",
                "sInfoThousands": ",",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "上页",
                    "sNext": "下页",
                    "sLast": "末页"
                },
                "oAria": {
                    "sSortAscending": ": 以升序排列此列",
                    "sSortDescending": ": 以降序排列此列"
                }
            },
            "fnCreatedRow": function (nRow, aData, iDataIndex) {
//                console.log(nRow)
            },
            "fnRowCallback": function (nRow, aaData, iDisplayIndex, iDisplayIndexFull) {
            }
        })
    }
    function deleteUser(btn){
//        alert("2")
        var id = $(btn).attr('data-id');
        $.ajax({
            url: 'order/deteleById',
            data:{
                id:id
            },
            method: 'post',
            success: function (res) {
                alert('成功')
                orderTableAjax()
            },
            error: function () {

            }

        })
    }
    function userUpdateModel(btn){
//        alert("2")
        var id = $(btn).attr('data-id');
        $.ajax({
            url: 'order/confirmById',
            data:{
                id:id
            },
            method: 'post',
            success: function (res) {
                alert('成功')
                orderTableAjax()
            },
            error: function () {

            }

        })
    }
    function chexiao(btn){
//        alert("2")
        var id = $(btn).attr('data-id');
        $.ajax({
            url: 'order/regretById',
            data:{
                id:id
            },
            method: 'post',
            success: function (res) {
                alert('成功')
                orderTableAjax()
            },
            error: function () {

            }

        })
    }

    function findName(data){
        var name
        $.ajax({
            url: 'user/findOneAction',
            data:{
                id:data
            },
            async:false,
            method: 'post',
            success: function (res) {
                console.log(res[0].name)
                name = res[0].name
            },
            error:function(res){
                name = name
            }
        })
        return name
    }
</script>